<template>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="审批状态" prop="approvalState">
        <el-select
          clearable
          v-model="queryParams.approvalState"
          placeholder="请选择"
          class="!w-200px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BID_APPROVAL_STATUS_SHOW)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          type="daterange"
          class="!w-250px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['bid-client:financeDeduct:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      border
      stripe
      :header-cell-style="{ background: '#F3F4F7', color: '#555' }"
    >
      <el-table-column label="编号" align="center" width="60">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="客户名称"
        align="center"
        prop="accountName"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="审批状态" align="center" prop="approvalState" width="150">
        <template #default="{ row }">
          <dict-tag :type="DICT_TYPE.BID_APPROVAL_STATUS_SHOW" :value="row.approvalState" />
        </template>
      </el-table-column>
      <el-table-column label="交易前余额(元)" align="center" prop="frontTransAmount" width="150" />
      <el-table-column label="申请退款金额(元)" align="center" prop="transAmount" width="150" />
      <el-table-column label="申请凭证" align="center" prop="deliveryCycle">
        <template #default="{ row }" width="120">
          <el-popover
            v-if="row.transVoucher?.url"
            :width="340"
            placement="top-start"
            trigger="click"
            popper-style="box-shadow: rgb(14 18 22 / 35%) 0px 10px 38px -10px, rgb(14 18 22 / 20%) 0px 10px 20px -15px; padding: 20px;"
          >
            <template #reference>
              <el-button link type="primary">查看</el-button>
            </template>
            <template #default>
              <el-image
                style="width: auto; height: 150px"
                :src="row.transVoucher?.url"
                :preview-src-list="[row.transVoucher?.url]"
              />
            </template>
          </el-popover>
          <span v-else>暂无图片</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="contactPhone" width="150" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="200"
        :formatter="dateFormatter"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        min-width="200"
        :show-overflow-tooltip="true"
      />
    </el-table>
    <div class="flex justify-between">
      <div class="text-lg mt-3 font-semibold">申请退款金额合计： {{ totalNum || 0 }}万元 </div>
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </ContentWrap>
</template>
<script setup lang="ts" name="refundRecord">
import * as Utils from '@/utils/dict'
import download from '@/utils/download'
import * as financial from '@/api/financial'
import { dateFormatter } from '@/utils/formatTime'

const { DICT_TYPE, getIntDictOptions } = reactive(Utils)
const loading = ref(true) // 列表的加载中
const exportLoading = ref(false) // 列表的加载中
const message = useMessage()
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const totalNum = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  approvalState: undefined,
  createTime: []
})
const queryFormRef = ref()

/** 查询列表 */

const getList = async () => {
  loading.value = true
  try {
    const data = await financial.financeDeductList({ approvalType: 'deductMoney', ...queryParams })
    list.value = data.list
    total.value = data.total
    totalNum.value = data.transAmountTotal
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await financial.exportFinanceDeductExcel(queryParams)
    download.excel(data, '退款记录列表.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})

defineExpose({})
</script>
